package com.petcommunity.dao;

import com.petcommunity.model.LostPet;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LostPetDAO {

    public void addLostPet(LostPet lostPet) {
        String sql = "INSERT INTO lost_pets (pet_id, lost_date, location, description, found) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, lostPet.getPetId());
            stmt.setDate(2, new java.sql.Date(lostPet.getLostDate().getTime()));
            stmt.setString(3, lostPet.getLocation());
            stmt.setString(4, lostPet.getDescription());
            stmt.setBoolean(5, lostPet.isFound());
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<LostPet> getAllLostPets() {
        List<LostPet> list = new ArrayList<>();
        String sql = "SELECT * FROM lost_pets ORDER BY lost_id ASC";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                LostPet lostPet = new LostPet();
                lostPet.setLostId(rs.getInt("lost_id"));
                lostPet.setPetId(rs.getInt("pet_id"));
                lostPet.setLostDate(rs.getDate("lost_date"));
                lostPet.setLocation(rs.getString("location"));
                lostPet.setDescription(rs.getString("description"));
                lostPet.setFound(rs.getBoolean("found"));
                list.add(lostPet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public LostPet getLostPetById(int id) {
        LostPet lostPet = null;
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM lost_pets WHERE lost_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                lostPet = new LostPet();
                lostPet.setLostId(rs.getInt("lost_id"));
                lostPet.setPetId(rs.getInt("pet_id"));
                lostPet.setLostDate(rs.getDate("lost_date"));
                lostPet.setLocation(rs.getString("location"));
                lostPet.setDescription(rs.getString("description"));
                lostPet.setFound(rs.getBoolean("found"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lostPet;
    }

    public void updateLostPet(LostPet lostPet) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE lost_pets SET pet_id=?, lost_date=?, location=?, description=?,found=? WHERE lost_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, lostPet.getPetId());
            ps.setDate(2, lostPet.getLostDate());
            ps.setString(3, lostPet.getLocation());
            ps.setString(4, lostPet.getDescription());
            ps.setBoolean(5, lostPet.isFound());
            ps.setInt(6, lostPet.getLostId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteLostPet(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM lost_pets WHERE lost_pets.lost_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
